<?php

namespace Admin\Controller;
/**广告控制器
 * Class AdController
 * @package Admin\Controller
 */
class AdController extends AdminController {


    /*
     * 广告位置
     */
    public function adPosition(){
        if(IS_POST){

        }else{
            $typeList = M('ad_position')->select();
            $lists =list_to_tree($typeList);//处理成分级的嵌套数组

            $this->assign('lists',$lists);
            $this->display();

        }
    }

    /*
     * 添加广告位置弹框
     */
    public function positionAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');

            //处理参数
            $data['position_name'] = trim(I('post.position_name'));
            if($data['position_name'] == ''){
                $this->error('广告位置名称不能为空');
            }
            if($post['position_desc']){
                $data['position_desc'] = $post['position_desc'];
            }

            //验证通过了
            $res = M('ad_position')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
    }
    /*
     * 修改广告位置弹框
     */
    public function positionEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            //处理参数
            $data['position_name'] = trim(I('post.position_name'));
            if($data['position_name'] == ''){
                $this->error('广告位置名称不能为空');
            }
            $data['position_desc'] = $post['position_desc'];

            $res = M('ad_position')->where(array('id'=>I('post.id')))->save($data);
            if(is_numeric($res)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $id = I('get.id');
            if($id){
                $data = M('ad_position')->where(array('id'=>I('get.id')))->find();
                $this->assign('data',$data);
                $this->display();
            }
        }
    }

    /*
     * 操作
     */
    public function updateStatus($method=null){
        if ( empty($_REQUEST['id']) ) {
            $this->error('请选择要操作的数据!');
        }
        switch ( strtolower($method) ){
            case 'deleteadposition':
                $this->remove('ad_position');
                break;
            case 'deletead':
                //删除图片
                $res = M('ad')->where(array('id'=>I('id')))->find();
                $path = $_SERVER['DOCUMENT_ROOT'].$res['ad_link'];
                @unlink($path);
                $this->remove('ad');
                break;
            default:
                $this->error($method.'参数非法');
        }
    }


    /*
     * 广告列表
     */
    public function adList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        $searchdata_ad = session('searchdata_adlist');

        if(IS_POST){
            session('searchdata_adlist',$get);
        }else{
            if(I('get.p')){
                $this->assign('p',I('get.p'));
            }else{
                session('searchdata_adlist',null);
            }
            $get = $search_data = session('searchdata_adlist');
        }
        if(isset($get['ad_name']) && $get['ad_name'] != ''){
            $search_data['ad_name'] = trim($get['ad_name']);
            $where['a.ad_name'] = array('like','%'.$search_data['ad_name'].'%');
        }
        if(isset($get['position_id']) && $get['position_id'] != ''){
            $search_data['position_id'] = $get['position_id'];
            $where['a.position_id'] = array('eq',$search_data['position_id']);
        }

        $count = M('ad')->alias('a')->where($where)->count();
        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        $p = I('get.p',"1");

        $lists = M('ad')
            ->alias('a')
            ->join('xinxi_ad_position b on a.position_id = b.id','left')
            ->field('a.*,b.position_name')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order("id desc ")
            ->select();
        $this->assign('lists',$lists);
        $this->assign("p",$p);
        $types = M("ad_position")->select();
        $this->assign('types',$types);
        $this->assign('searchs',$search_data);
        $this->display();
    }


    /*
     * 添加广告
     */
    public function adAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $data = I('post.');
            if($data['tid'] == 0 && $data['url'] == ""){
//                $this->error("请定义跳转产品或者跳转链接");
            }
            if($_FILES){
                $path = "/Uploads/Ads/" . date("Y-m-d") . "/";
                $res = $this->upload_img($_FILES,$path);
                if($res){
                    $data['ad_link'] = $res;
                }
            }
            if($data['url'] != ""){
                $data['tid'] = 0;
            }
            $res = M('ad')->add($data);
            if($res){
                $this->success('添加广告成功');
            }else{
                if($data['ad_link']){
                    $destination = $_SERVER['DOCUMENT_ROOT'].$data['ad_link'];
                    @unlink($destination);
                }
                $this->error('添加广告失败');
            }
        }else{
            $positionList = M('ad_position')->select();
            $this->assign('types',$positionList);
            $products = M('product')->field('id,name')->where(array('status'=>1))->select();
            $this->assign('products',$products);
            $this->display();
        }
    }

    /*
     * 编辑广告
     */
    public function adEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $data = I('post.');

            $id = $data['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
            if($data['tid'] == 0 && $data['url'] == ""){
//                $this->error("请定义跳转产品或者跳转链接");
            }

            if($_FILES) {//修改了图片
                $info = M('ad')->where(array('id' => $id))->find();
                //删除原图
                $path = $_SERVER['DOCUMENT_ROOT'] . $info['ad_link'];
                @unlink($path);
                $path = "/Uploads/Ads/" . date("Y-m-d") . "/";
                $res = $this->upload_img($_FILES, $path);
                if ($res) {
                    $data['ad_link'] = $res;
                }
            }

            unset($data['id']);

            if($data['url'] != ""){
                $data['tid'] = 0;
            }

            $res = M('ad')->where(array('id'=>$id))->save($data);

            if(is_numeric($res)){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }else{
            $res = M('ad')->where(array('id'=>I('id')))->find();
            $this->assign('res',$res);
            $typeList = M('ad_position')->select();
            $this->assign('types',$typeList);
            $products = M('product')->field('id,name')->where(array('status'=>1))->select();
            $p = I('get.p',"1");
            $this->assign('products',$products);

            $this->assign('p',$p);
            $this->display();
        }
    }
	
	
	/*
     * 视频广告列表
     */
    public function videoList(){
		
		$limit = LIMIT;


        $where['status'] = array('eq',1);

        $count = M('ad_video')->where($where)->count();
        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        $p = I('get.p',"1");

        $lists = M('ad')
            ->alias('a')
            ->join('xinxi_ad_position b on a.position_id = b.id','left')
            ->field('a.*,b.position_name')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order("id desc ")
            ->select();
        $this->assign('lists',$lists);
        $this->assign("p",$p);
        $types = M("ad_position")->select();
        $this->assign('types',$types);
        $this->assign('searchs',$search_data);
        $this->display();
		
	}
}
